package cn.jianyongjie.util;

import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ESClientUtil {

    // 连接es的工具
    public static TransportClient getClient(){
        // 客户端
        TransportClient client = null;
        // 配置集群的名字
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch").build();
        try {
            // 创建客户端
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }


    /*
    * 复杂查询工具
    * index：指定index进行搜索
    * type：指定type搜索
    * from：当前页码
    * size：每页展示条数
    * age:查询条件
    * let:最大查询条件
    * gte：最小查询条件
    * name:查询的key值
    * answer:查询的value值
    * */
    public static void search(String index
            ,String type
            ,int from
            ,int size
            ,String sort
            ,int let,int get
            ,String name
            ,String answer){

        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();

        // 搜索请求，构建器,指定index进行搜索
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
        // type
        searchRequestBuilder.setTypes(type);
        // 当前页码
        searchRequestBuilder.setFrom(from);
        // 每页展示条数
        searchRequestBuilder.setSize(size);
        // 排序方式
        searchRequestBuilder.addSort(sort, SortOrder.ASC);

        //查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        List<QueryBuilder> must = boolQueryBuilder.must();
        must.add(QueryBuilders.matchQuery(name , answer));

        List<QueryBuilder> filter = boolQueryBuilder.filter();
        // 根据什么进行查询，最大查询数和最小查询数
        filter.add(QueryBuilders.rangeQuery(sort).lte(let).gte(get));
        // filter.add(QueryBuilders.termQuery("sex",1));

        // 将query放入到搜索请求
        searchRequestBuilder.setQuery(boolQueryBuilder);
        System.out.println(searchRequestBuilder);
        //
        SearchResponse searchResponse = searchRequestBuilder.get();

        SearchHits hits = searchResponse.getHits();

        // 循环遍历数据
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsMap());

        }

        client.close();
    }




    /*
    * @Title: ESClientUtil
    * @Description: 删除工具
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/24 18:40
    * @Parameters: * @param
    * @Return void
    */
    public static void delete(String index,String type,String id){
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();

        // 根据index和type和id删除数据
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(index, type, id);
        deleteRequestBuilder.get();
        System.out.println(deleteRequestBuilder);

        client.close();
    }



    /*
    * @Title: ESClientUtil
    * @Description: 新增工具
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/24 18:56
    * @Parameters: * @param
    * @Return void
    *
    * index：指定index进行新增
    * type：指定type新增
    * id：设置新增id
    * name：设置新增的名称
    * age：新增的名字
    */
    public static void add(String index,String type,String id,String name,int age) {
        //获取客户端对象
        TransportClient client = ESClientUtil.getClient();

        //创建索引，type，id
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index, type, id);

        // 新建一个map
        Map<String,Object> data = new HashMap<String,Object>();
        // 设置新增的参数
        data.put("id",id);
        data.put("name",name);
        data.put("age",age);

        // 进行新增
        indexRequestBuilder.setSource(data).get();
        client.close();
    }



    /*
    * @Title: ESClientUtil
    * @Description: 根据id修改
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/24 19:21
    * @Parameters: * @param index
     * @param type
     * @param id
     * @param name
     * @param age
    * @Return void
    */
    public static void updete(String index,String type,String id,String name,int age) {


        // 获取es对象
        TransportClient client = ESClientUtil.getClient();

        //修改索引
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(index, type, id);
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("id",id);
        data.put("name",name);
        data.put("age",age);
        //获取结果设置修改内容
        updateRequestBuilder.setDoc(data).get();


        client.close();
    }


    /*
    * @Title: ESClientUtil
    * @Description: 根据id查询数据
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/24 22:35
    * @Parameters: * @param index
     * @param type
     * @param id
     * @param name
     * @param age
    * @Return void
    */
    public static void findById(String id) {


        // 创建es对象
        TransportClient client = ESClientUtil.getClient();
        // 输入index、type、查询的id
        GetRequestBuilder getRequestBuilder = client.prepareGet("pet", "pet", id);
        // 调用查询方法
        GetResponse documentFields = getRequestBuilder.get();
        // 获取查询数据
        System.out.println(documentFields.getSource());
    }




    /*
    * @Title: ESClientUtil
    * @Description: 查询所有
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/25 8:59
    * @Parameters: * @param id
    * @Return void
    */
    public static SearchResponse finds(String index,String type) {


        // 创建es对象
        TransportClient client = ESClientUtil.getClient();
        // 输入index、type、查询的id
        SearchRequestBuilder pet = client.prepareSearch(index);
        pet.setTypes(type);

        // 调用查询方法
        return pet.get();
    }


}